package com.zhouzhou.fastjava.util;

import org.apache.commons.lang3.StringUtils;

public abstract class UnicodeKit {

    private UnicodeKit() {
    }

    /**
     * 将unicode码转化成字符串
     */
    public static String unicode2String(String unicode) {
        if (StringUtils.isBlank(unicode)) {
            return null;
        }

        StringBuilder sb = new StringBuilder();
        int i;
        int pos = 0;

        while ((i = unicode.indexOf("\\u", pos)) != -1) {
            sb.append(unicode, pos, i);
            if (i + 5 < unicode.length()) {
                pos = i + 6;
                sb.append((char) Integer.parseInt(unicode.substring(i + 2, i + 6), 16));
            }
        }
        //如果pos位置后，有非中文字符，直接添加
        sb.append(unicode.substring(pos));
        return sb.toString();
    }

}
